package com.weiawei.ui;

import com.weiawei.component.BackGroundPannel;
import com.weiawei.util.PathUtils;
import com.weiawei.util.ScreenUtils;
import com.weiawei.util.shujuqidongUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class MainInterface {

    //定义窗口的大小
    final int width = 1000;
    final int height = 600;
    JFrame jf = new JFrame("巨好玩的五子棋");

    shujuqidongUtils shuju1 = new shujuqidongUtils();

    // 组装视图
    public void init() throws IOException {

        shujuqidongUtils qidong = new shujuqidongUtils();
        qidong.chushihua();

        // 设置窗口相关的属性 位置+长宽高
        jf.setBounds((ScreenUtils.getScreenWidth() - width) / 2, (ScreenUtils.getScreenHeight() - height) / 2, width, height);
        // 窗口大小不可变
        jf.setResizable(false);
        // 设定窗口图标
        jf.setIconImage(ImageIO.read(new File(PathUtils.iconpath)));

        //设置窗口的内容
        BackGroundPannel bgpannel = new BackGroundPannel(ImageIO.read(new File(PathUtils.backgroundpath)));

        // 组装登录相关的元素
        Box vbox = Box.createVerticalBox();

        // 组装用户名
        Box usernamebox = Box.createHorizontalBox();  // 单独创建一个box存储用户名

        JLabel ulabel = new JLabel("用户名：");  // 贴标签
        JTextField uField = new JTextField(15);  // 贴输入行

        usernamebox.add(ulabel);
        usernamebox.add(Box.createHorizontalStrut(20));//装空格隔开 调整格式
        usernamebox.add(uField);

        //组装密码
        Box passwordbox = Box.createHorizontalBox();
        JLabel plabel = new JLabel("密   码：");
        JTextField pField = new JTextField(15);

        passwordbox.add(plabel);
        passwordbox.add(Box.createHorizontalStrut(20));
        passwordbox.add(pField);

        //组装按钮
        Box btnbox = Box.createHorizontalBox();
        JButton loginBtn = new JButton("登录");
        JButton registBtn = new JButton("注册");

        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取用户输入的数据
                //监听对应的文本 获取数据
                String username = uField.getText().trim();
                String password = pField.getText().trim();
                //访问登录接口
                try {
                    shuju1.chushihua();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }

                int denglu = shuju1.denglu(username, password);

                if(denglu == 1)
                {
                    JOptionPane.showMessageDialog(jf,"该用户不存在，请先去注册用户");
                }else if(denglu == 2){
                    JOptionPane.showMessageDialog(jf,"登录成功！！！");
                    new InitialMenu();
                    jf.dispose();

                }else{
                    JOptionPane.showMessageDialog(jf,"登陆失败,用户名或密码错误");
                }

            }
        });

        registBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //TODO 跳转到注册页面
               // JOptionPane.showMessageDialog(jf,"跳转到注册页面");
                try {
                    new RegisterInterface().init();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                //当前界面消失
                jf.dispose();

            }
        });


        btnbox.add(loginBtn);
        btnbox.add(Box.createHorizontalStrut(100));
        btnbox.add(registBtn);

        vbox.add(Box.createVerticalStrut(200));
        vbox.add(usernamebox);
        vbox.add(Box.createVerticalStrut(40));
        vbox.add(passwordbox);
        vbox.add(Box.createVerticalStrut(80));
        vbox.add(btnbox);

        
        bgpannel.add(vbox);
        jf.add(bgpannel);

        //设置窗口可见
        jf.setVisible(true);

    }

    public static void main(String[] args) throws IOException {
        new MainInterface().init();
    }


}
